package cn.vc.communityservice.controller;

import cn.vc.basicmodule.entity.Result;
import cn.vc.communityservice.entity.Community;
import cn.vc.communityservice.service.CommunityService;
import cn.vc.feign.userclient.entity.User;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/community")
public class CommunityController {
    private final CommunityService communityService;
    private final RedisTemplate redisTemplate;
    private final HttpServletRequest request;

    public CommunityController(CommunityService communityService, RedisTemplate redisTemplate, HttpServletRequest request) {
        this.communityService = communityService;
        this.redisTemplate = redisTemplate;
        this.request = request;
    }



    /**
     * <h2>创建社区</h2>
     * @param name
     * @param intro
     * @param tag
     * @return
     */
    @PostMapping()
    public Result<?> found(@RequestParam String name, @RequestParam String intro, @RequestParam String tag) {
        User myUser = redisTemplate.opsForValue().get(request.getHeader("token"))!=null?JSON.parseObject(redisTemplate.opsForValue().get(request.getHeader("token")).toString(), User.class):null;
        if (myUser==null) return new Result<>(0);
        System.out.println("用户 -> "+myUser);
        Community community = new Community(myUser.getId(), name, intro, tag);
        boolean bo = communityService.found(community);
        return new Result<>(bo);
    }



    /**
     * <h2>删除社区</h2>
     * @param id 需要删除的社区ID
     * @return
     */
    @DeleteMapping
    public Result<?> delete(@RequestParam Long id) {
        User myUser = redisTemplate.opsForValue().get(request.getHeader("token"))!=null?JSON.parseObject(redisTemplate.opsForValue().get(request.getHeader("token")).toString(), User.class):null;
        if (myUser==null) return new Result<>(0);
        boolean bo = communityService.delete(id, myUser.getId());
        return new Result<>(bo);
    }



    /**
     * <h2>修改社区</h2>
     * @param id
     * @param name
     * @param intro
     * @param tag
     * @return
     */
    @PutMapping
    public Result<?> update(@RequestParam Long id, @RequestParam String name, @RequestParam String intro, @RequestParam String tag) {
        User myUser = redisTemplate.opsForValue().get(request.getHeader("token"))!=null?JSON.parseObject(redisTemplate.opsForValue().get(request.getHeader("token")).toString(), User.class):null;
        if (myUser==null) return new Result<>(0);
        Community community = communityService.selectById(id);
        if (community==null || !community.getFounderId().equals(myUser.getId())) return new Result<>(0);
        community.setName(name);
        community.setIntro(intro);
        community.setTag(tag);
        boolean bo = communityService.update(community);
        return new Result<>(bo);
    }
}